﻿using System;
using System.Collections.Generic;
using System.Web;
//using SubSonic;
using Newtonsoft.Json.Linq;
using Model;
using Manager;

namespace Manager.CustomerHelper
{
    public static class RoleModulePowerHelper
    {
        private static Dictionary<int, Dictionary<string, RoleModulePower>> _roleModulePowerDic = new Dictionary<int, Dictionary<string, RoleModulePower>>();

        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <param name="roleId"></param>
        public static void UpdateRoleModulePowerDic(int roleId)
        {
            if (_roleModulePowerDic.ContainsKey(roleId))
            {
                _roleModulePowerDic.Remove(roleId);
            }
        }

        /// <summary>
        /// 获取角色对应的全部模块权限列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public static Dictionary<string, RoleModulePower> GetRoleModulePowerDic(int roleId)
        {
            if (!_roleModulePowerDic.ContainsKey(roleId))
            {

                //SqlQuery q = new Select().From<XMenu>();
                //q.Where("1").IsEqualTo("1");

                //// 名称不是AppModule.None
                //q.And(XMenu.NameColumn).IsNotEqualTo(ModuleTypeHelper.Module2String(ModuleType.None));

                //XMenuCollection items = q.ExecuteAsCollection<XMenuCollection>();
                //X_MenuBLL BLL = new X_MenuBLL();
                MenuManager manager = new MenuManager();
                List<Menu> items = manager.NameFilter(ModuleTypeHelper.Module2String(ModuleType.None));
                List<RoleModulePower> roleModules = RoleModulePowerHelper.GetRoleModulePowers(roleId, items);

                _roleModulePowerDic[roleId] = new Dictionary<string, RoleModulePower>();
                foreach (RoleModulePower roleModule in roleModules)
                {
                    _roleModulePowerDic[roleId].Add(roleModule.Name, roleModule);
                }
            }

            return _roleModulePowerDic[roleId];

        }

        public static List<RoleModulePower> GetRoleModulePowers(int roleId,List<Menu> modules)
        {
            List<RoleModulePower> myRoleModules = new List<RoleModulePower>();

            Dictionary<int, Sys_RoleModule> roleModuleDic = GetRoleModuleDictionary(roleId);

            foreach (Menu module in modules)
            {
                RoleModulePower myRoleModule = new RoleModulePower();
                myRoleModule.Title = module.Title;
                myRoleModule.Name = module.MenuName;
                myRoleModule.RoleId = roleId;
                myRoleModule.ModuleId = module.MenuId;

                // 默认权限都是false
                myRoleModule.CanRead = false;
                //myRoleModule.CanAdd = false;
                //myRoleModule.CanEdit = false;
                //myRoleModule.CanDelete = false;

                myRoleModule.OtherPowers = new JObject();
                List<string> otherPowerNames = ModulePowerTypeHelper.GetModuleOtherPowerTypeDic(module.MenuName);
                foreach (string powerName in otherPowerNames)
                {
                    myRoleModule.OtherPowers.Add(powerName, JToken.FromObject(false));
                }


                // 数据库存储的值
                if (roleModuleDic.ContainsKey(module.MenuId))
                {
                    Sys_RoleModule roleModule = roleModuleDic[module.MenuId];
                    myRoleModule.CanRead = roleModule.CanRead;
                    //myRoleModule.CanAdd = roleModule.CanAdd;
                    //myRoleModule.CanEdit = roleModule.CanEdit;
                    //myRoleModule.CanDelete = roleModule.CanDelete;
                    //myRoleModule.Others = roleModule.Others;

                    if (!String.IsNullOrEmpty(roleModule.Others) && roleModule.Others != "{}")
                    {
                        JObject dbOtherPowerObj = JObject.Parse(roleModule.Others);

                        IEnumerator<KeyValuePair<string, JToken>> enumerator = dbOtherPowerObj.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            KeyValuePair<string, JToken> current = enumerator.Current;
                            myRoleModule.OtherPowers[current.Key] = current.Value;
                        }
                    }
                }


                myRoleModules.Add(myRoleModule);
            }

            return myRoleModules;
        }


        private static Dictionary<int, Sys_RoleModule> GetRoleModuleDictionary(int roleId)
        {
        //    SqlQuery q = new Select().From<XRoleModule>();
        //    q.Where("1").IsEqualTo("1");

        //    q.And(X_RoleModule.RoleIdColumn).IsEqualTo(roleId);

        //    XRoleModuleCollection roleModules = q.ExecuteAsCollection<XRoleModuleCollection>();
            Sys_RoleModuleManager manager = new Sys_RoleModuleManager();

            IList<Sys_RoleModule> roleModules = manager.GetByRole(roleId);
            Dictionary<int, Sys_RoleModule> roleModuleDic = new Dictionary<int, Sys_RoleModule>();
            foreach (Sys_RoleModule roleModule in roleModules)
            {
                roleModuleDic.Add(roleModule.ModuleId, roleModule);
            }

            return roleModuleDic;
        }



    }
}
